home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / arts / resample.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  4.5 KB  |  168 lines

  1.     /*
  2.  
  3.     Copyright (C) 2000 Stefan Westerfeld
  4.                        stefan@space.twc.de
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.   
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.    
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.     Boston, MA 02111-1307, USA.
  20.  
  21.     */
  22.  
  23.  
  24. #ifndef ARTS_FLOW_RESAMPLE_H
  25. #define ARTS_FLOW_RESAMPLE_H
  26.  
  27. #include "arts_export.h"
  28.  
  29. /*
  30.  * BC - Status (2002-03-08): Resampler / Refiller.
  31.  *
  32.  * These classes will be kept binary compatible. Resampler has a private
  33.  * data pointer for this purpose.
  34.  */
  35.  
  36. namespace Arts {
  37.  
  38. /**
  39.  * Refiller interface
  40.  *
  41.  * this is used by the resampler class
  42.  */
  43. class ARTS_EXPORT Refiller {
  44. public:
  45.     /**
  46.      * callback method to supply data to the Resampler
  47.      *
  48.      * here you supply data in the encoding specified in the Resampler class
  49.      * (bits, channels, endianness) to the Resampler
  50.      *
  51.      * @param buffer a pointer to a buffer where the data can be filled to
  52.      * @param len the size of the buffer in bytes
  53.      *
  54.      * @returns the number of bytes really available
  55.      */
  56.     virtual unsigned long read(unsigned char *buffer, unsigned long len) = 0;
  57.     virtual ~Refiller();
  58. };
  59.  
  60. class ResamplerPrivate;
  61.  
  62. /**
  63.  * The Resampler class resamples data from an input stream, which is read
  64.  * through the Refiller interface, and renders it into the float format which
  65.  * aRts usually uses, converting the sampling rate and format as needed.
  66.  */
  67. class ARTS_EXPORT Resampler {
  68. protected:
  69.     static const unsigned int bufferSize = 256;        //  64 samples in buffer
  70.     static const unsigned int bufferWrap = 64;        // + "wrap around"
  71.     /*
  72.      * BC: bufferWrap just needs channels * (filtersize - 1) * bytes per sample
  73.      * bytes wrap around - this used to be 4: 2 channels, linear interpolation,
  74.      * 16bit. However, for supporting float resampling and/or better filters,
  75.      * we'll make this 64 to be safe.
  76.      */
  77.  
  78.     int bufferSamples;
  79.     int sampleSize;
  80.     int dropBytes;
  81.  
  82.     Refiller *refiller;
  83.     double pos, step;
  84.     int channels,bits;
  85.  
  86.     unsigned char buffer[bufferSize+bufferWrap];
  87.     float fbuffer[bufferSize+bufferWrap];
  88.     long block, haveBlock;
  89.  
  90.     class ResamplerPrivate *d;
  91.  
  92.     void updateSampleSize();
  93.     void ensureRefill();
  94. public:
  95.     /**
  96.      * constructor
  97.      *
  98.      */
  99.     Resampler(Refiller *refiller);
  100.     ~Resampler();
  101.  
  102.     /**
  103.      * sets the resampling step in the input stream
  104.      *
  105.      * for instance, settings step to 2.0, the file would be played twice as
  106.      * fast - you can calculate the resampling step from the sampling rate
  107.      * the stream you resample was recorded at, and the sampling rate you want
  108.      * it to be resampled to, using
  109.      *
  110.      *   step = originalSamplingRate / desiredSamplingRate
  111.      *
  112.      * for instance, if a stream recorded at 22050 Hz is to be played at 48000
  113.      * Hz, step would be
  114.      *
  115.      *   step = 22050 / 48000 = 0.46
  116.      */
  117.     void setStep(double step);
  118.  
  119.     /**
  120.      * sets the number of channels in the input stream
  121.      *
  122.      * the channels are expected to be interleaved, thus if you have two
  123.      * channels, you will have one sample for the left stream, one for the
  124.      * right, one for the left, ... and so on
  125.      *
  126.      * currently only either mono (channels = 1) or stereo (channels = 2)
  127.      * streams are supported
  128.      */
  129.     void setChannels(int channels);
  130.  
  131.     /**
  132.      * sets the number of bits in the input stream
  133.      *
  134.      * currently, only 16 bit and 8 bit streams are supported
  135.      */
  136.     void setBits(int bits);
  137.  
  138.     enum Endianness { bigEndian, littleEndian };
  139.  
  140.     /**
  141.      * sets the endianess of the input stream
  142.      *
  143.      * supported are bigEndian and littleEndian
  144.      */
  145.     void setEndianness(Endianness endianness);
  146.  
  147.     /**
  148.      * renders a buffer of float samples from the input stream
  149.      *
  150.      * the data is obtained by querying the refiller for data from the
  151.      * input stream and filling them into the float array pointed to by
  152.      * left and right - if reading data fails, the underrun flag is set
  153.      */
  154.     void run(float *left, float *right, unsigned long samples);
  155.  
  156.     /**
  157.      * query the underrun flag
  158.      *
  159.      * underruns occur if run is called, but no data is available from the
  160.      * Refiller object
  161.      */
  162.     bool underrun();
  163. };
  164.  
  165. }
  166.  
  167. #endif /* ARTS_FLOW_RESAMPLE_H */
  168.